<?php

namespace App\Api;

use App\Http\Controllers\Controller;
use App\Models\ModuleConfig;
use Illuminate\Http\Request;
use App\Models\Site;
use App\Models\Module;
use Illuminate\Contracts\Container\BindingResolutionException;

/**
 * 模块配置
 * @package App\Http\Controllers
 */
class ModuleConfigController extends Controller
{
    public function __construct()
    {
        $this->middleware(['module', 'auth:sanctum']);
    }

    /**
     * 更新模块配置项
     * @param Request $request
     * @param Site $site
     * @param Module $module
     * @return array
     * @throws BindingResolutionException
     */
    public function update(Request $request, Site $site, Module $module)
    {
        $config = ModuleConfig::where('site_id', $site->id)->where('module_id', $module->id)->value('config') ?? [];
        $config = request()->input() + $config;

        ModuleConfig::updateOrCreate([
            'site_id' => $site->id,
            'module_id' => $module->id
        ], ['config' => $config]);
        return $this->message('配置修改成功');
    }
}
